package cn.com.dycc.topic_activity.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.dycc.common.log.Constants;
import cn.com.dycc.common.log.SystemServiceLog;
import cn.com.dycc.party_org.organize.dao.OrganizeDao;

import cn.com.dycc.party_org.organize.param.PartyInfoParam;
import cn.com.dycc.party_org.organize.service.OrganizeService;
import cn.com.dycc.sys_manage.deptsysmanagement.dao.DeptDao;
import cn.com.dycc.topic_activity.dao.Pb_Activity_VoteDao;
import cn.com.dycc.topic_activity.entity.Pb_Activity_Vote;

import cn.com.dycc.topic_activity.param.Pb_Activity_VoteParam;

import cn.com.dycc.util.DateUtil;
import cn.com.dycc.util.JsonObjectResult;
import cn.com.dycc.util.PageObjectUtil;
import cn.com.dycc.util.StringUtil;


@Service("pb_Activity_VoteService")
@Transactional
public class Pb_Activity_VoteServiceImpl implements Pb_Activity_VoteService{

	@Resource
	private Pb_Activity_VoteDao pb_Activity_VoteDao;
	
	@Resource
	private DeptDao deptDao;
	
	@Resource
	private OrganizeDao organizeDao;
	
	@Resource
	private OrganizeService organizeService;
	
	
	@SystemServiceLog(description=Constants.ACTIVITY_VOTE_ADD)
	public int addPb_Activity_Vote(Pb_Activity_VoteParam param, HttpServletRequest request)
			throws Pb_Activity_VoteNotFoundException {
		checkParam(param);
		if(param.getActivity_id()==null||param.getActivity_id().trim().isEmpty()){
			throw new Pb_Activity_VoteNotFoundException("请选择需要点赞的专题活动");
		}
		HttpSession session=request.getSession();
		String user_id=(String)session.getAttribute("id");
		String vote_date=DateUtil.getNowTimestamp().toString();
		String org_id=(String)session.getAttribute("org_id");
		String dept_id=(String)session.getAttribute("dept_id");
		if(dept_id==null||dept_id.trim().isEmpty()){
			throw new Pb_Activity_VoteNotFoundException("该用户不是党员不能点赞");
		}
		param.setDept_id(dept_id);
		param.setVote_user_id(user_id);
		param.setVote_date(vote_date);
		param.setState("Y");
		param.setOrg_id(org_id);
		//先判断点赞人是否对专题活动点过赞
		Pb_Activity_VoteParam param2=new Pb_Activity_VoteParam();
		param2.setActivity_id(param.getActivity_id());
		param2.setVote_user_id(user_id);
		param2.setVote_date(vote_date);
		param2.setVote_date_e(vote_date);
		String count1=pb_Activity_VoteDao.getPb_Activity_VoteCountByParam(param2);
		if(!"0".equals(count1)){
			throw new Pb_Activity_VoteNotFoundException("今日专题活动已点过赞,不可重复点赞");
		}
		param2.setActivity_id(null);
		/*param2.setVote_date(vote_date);
		param2.setVote_date_e(vote_date);*/
		String count2=pb_Activity_VoteDao.getPb_Activity_VoteCountByParam(param2);
		if(5-Integer.valueOf(count2)<=0){
			throw new Pb_Activity_VoteNotFoundException("今日已点赞5个不同的专题活动,不能在点赞其他的专题活动");
		}
		List<Pb_Activity_Vote> lists = getServiceList(param);
		Pb_Activity_Vote entity=lists.get(0);
		int d = pb_Activity_VoteDao.savePb_Activity_Vote(entity);
		
		
		return d;
		
	}

	

	

	
	
	
	
	/**
	 * 校验 @param param @return @throws
	 */
	public void checkParam(Pb_Activity_VoteParam param) {
		if (param == null) {
			throw new Pb_Activity_VoteNotFoundException("未登录或超时,请重新登录");
		}
		
		

	}

	public List<Pb_Activity_Vote> getServiceList(Pb_Activity_VoteParam param) {
		Pb_Activity_Vote entity = new Pb_Activity_Vote();

		entity.setVote_id(param.getVote_id());
		entity.setActivity_id(param.getActivity_id());
		entity.setVote_user_id(param.getVote_user_id());
		if(param.getVote_date()!=null&&!param.getVote_date().trim().isEmpty()){
			entity.setVote_date(Timestamp.valueOf(param.getVote_date()));
		}
		entity.setDept_id(param.getDept_id());
		entity.setVote_des(param.getVote_des());
		entity.setState(param.getState());
		entity.setOrg_id(param.getOrg_id());
		
		
		List<Pb_Activity_Vote> lists = new ArrayList<Pb_Activity_Vote>();
		lists.add(entity);
		return lists;
	}







	@SystemServiceLog(description=Constants.ACTIVITY_VOTE_DELETE)
	public int deletePb_Activity_Vote(String[] attrStrs, HttpServletRequest request)
			throws Pb_Activity_VoteNotFoundException {
		if (attrStrs == null) {
			throw new Pb_Activity_VoteNotFoundException("请选择要删除的专题活动成果汇报信息");
		}
		int d = pb_Activity_VoteDao.deletePb_Activity_Vote(attrStrs);
		return d;
	}









	public JsonObjectResult findPb_Activity_VoteByParam(Pb_Activity_VoteParam param, HttpServletRequest request)
			throws Pb_Activity_VoteNotFoundException {
		List<Pb_Activity_Vote> lists = new ArrayList<Pb_Activity_Vote>();
		
		checkParam(param);
		//20180802 start
		HttpSession session=request.getSession();
		String dept_id=(String)session.getAttribute("dept_id");
		if(dept_id==null){
			throw new Pb_Special_ActivityNotFoundException("该用户不是党员");
		}
		
		if(param.getVote_user_id()==null&&"1".equals(param.getLeader_flag())){
			
			PartyInfoParam partyInfoParam=new PartyInfoParam(); 
			String party_dept=dept_id;
			List<String> dept_party_list=organizeService.findTreeByPidLine(request, partyInfoParam, party_dept);
			if(!dept_party_list.isEmpty()){
				param.setDept_party_list(dept_party_list);
			}else {
				throw new Pb_Special_ActivityNotFoundException("该部门及其子部门下无相应的党支部信息");
			}
			System.out.println("dept_party_list:"+dept_party_list);
		}
		//20180802 end
		param = realParam(param, request);
		String count = pb_Activity_VoteDao.getPb_Activity_VoteCountByParam(param);
		
		if ("0".equals(count)) {			
			return new JsonObjectResult(lists);
		}
		param = (Pb_Activity_VoteParam) PageObjectUtil.getSqlPageObject(count, param);
		lists = pb_Activity_VoteDao.findPb_Activity_VoteByParam(param);
		
		
		return new JsonObjectResult(count,lists);
	}
	
	/**
	 * 实际参数
	 * 
	 * @param param
	 * @param request
	 * @return
	 */
	public Pb_Activity_VoteParam realParam(Pb_Activity_VoteParam param, HttpServletRequest request) {
		if(param.getVote_des()!=null&&!param.getVote_des().trim().isEmpty()){
			param.setVote_des(StringUtil.format_mh_str(param.getVote_des()));
		}
		if(param.getActivity_name()!=null&&!param.getActivity_name().trim().isEmpty()){
			param.setActivity_name(StringUtil.format_mh_str(param.getActivity_name()));
		}
		if(param.getUser_name()!=null&&!param.getUser_name().trim().isEmpty()){
			param.setUser_name(StringUtil.format_mh_str(param.getUser_name()));
		}
		if(param.getOrder()==null||param.getOrder().isEmpty()||"1".equals(param.getOrder())){
			param.setOrder("p.vote_date desc");
		}else if("2".equals(param.getOrder())){
			param.setOrder("p.vote_date asc");
			
		}else{
			param.setOrder(null);//手动防止SQL注入
		}
		
		return param;
	}
	
	
}
